/*
 * Wazuh Vulnerability scanner - Scan Orchestrator
 * Copyright (C) 2015, Wazuh Inc.
 * March 11, 2024.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#ifndef _EVENT_DELETE_INVENTORY_HPP
#define _EVENT_DELETE_INVENTORY_HPP

#include "chainOfResponsability.hpp"
#include "inventorySync.hpp"
#include "scanContext.hpp"

/**
 * @brief TEventDeleteInventory class.
 * This class is in charge of deleting the inventory entries for a given agent and affected component type.
 * It receives the scan context and the inventory database and returns the scan context with the inventory cleaned.
 * The affected component type can be Os or Package.
 * Os: Delete inventory of the OS for the agent.
 * Package: Delete a package from the inventory for the agent.
 *
 * @tparam TScanContext scan context type.
 */
template<typename TScanContext = ScanContext>
class TEventDeleteInventory final
    : public AbstractHandler<std::shared_ptr<TScanContext>>
    , public TInventorySync<TScanContext>
{
public:
    // LCOV_EXCL_START
    /**
     * @brief EventDeleteInventory constructor.
     *
     * @param inventoryDatabase Inventory database.
     */
    explicit TEventDeleteInventory(Utils::RocksDBWrapper& inventoryDatabase)
        : TInventorySync<TScanContext>(inventoryDatabase)
    {
    }
    // LCOV_EXCL_STOP

    /**
     * @brief Handles request and passes control to the next step of the chain.
     *
     * @param data Scan context.
     * @return std::shared_ptr<ScanContext> Abstract handler.
     */
    std::shared_ptr<TScanContext> handleRequest(std::shared_ptr<TScanContext> data) override
    {
        std::string key;
        key.append(data->agentNodeName());
        key.append("_");
        key.append(data->agentId());
        key.append("_");

        // Create the key for the inventory.
        key.append(TInventorySync<TScanContext>::affectedComponentKey(data));

        const auto& column = AFFECTED_COMPONENT_COLUMNS.at(data->affectedComponentType());
        std::string value;

        if (TInventorySync<TScanContext>::m_inventoryDatabase.get(key, value, column))
        {
            auto listCve = Utils::split(value, ',');
            for (const auto& cve : listCve)
            {
                std::string elementKey;
                elementKey.append(key);
                elementKey.append("_");
                elementKey.append(cve);

                data->m_elements.emplace(cve, TInventorySync<TScanContext>::buildElement("DELETED", elementKey));
            }
            logDebug2(WM_VULNSCAN_LOGTAG, "Deleting %s agent key: %s", column.c_str(), key.c_str());
            TInventorySync<TScanContext>::m_inventoryDatabase.delete_(key, column);
        }

        return AbstractHandler<std::shared_ptr<TScanContext>>::handleRequest(std::move(data));
    }
};

using EventDeleteInventory = TEventDeleteInventory<>;

#endif // _EVENT_DELETE_INVENTORY_HPP
